﻿using _8Group.MES.MengIService;
using _8Group.MES.MengIService.DTO;
using _8Group.MES.Model.DV设备管理;
using _8Group.MES.Model.WM仓储管理;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace _8Group.MES.MengService
{
    [ApiExplorerSettings(GroupName = "孟祥宇")]
    public class DvCheckSubjectService : ApplicationService, IDvCheckSubjectService
    {
        private readonly IRepository<DV_Check_Subject, int> _dvCheckSubjectRepository;
        public DvCheckSubjectService(IRepository<DV_Check_Subject, int> dvCheckSubjectRepository)
        {
            _dvCheckSubjectRepository = dvCheckSubjectRepository;
        }

        public async Task<string> GetCheckSubjectAsync(string? subjectCode, string? subjectName, string? subjectType, int page, int limit)
        {
            var stock = await _dvCheckSubjectRepository.GetQueryableAsync();

            stock = stock.WhereIf(!string.IsNullOrEmpty(subjectCode), x => x.SubjectCode.Contains(subjectCode));
            stock = stock.WhereIf(!string.IsNullOrEmpty(subjectName), x => x.SubjectName.Contains(subjectName));
            stock = stock.WhereIf(!string.IsNullOrEmpty(subjectType), x => x.SubjectType.Contains(subjectType));

            int totalCount = stock.Count();

            stock = stock.OrderByDescending(c => c.Id).Skip((page - 1) * limit).Take(limit);

            return JsonConvert.SerializeObject(new { code = "0", data = stock.ToList(), count = totalCount, msg = "success" }, Formatting.Indented);
        }

        public async Task<int> AddCheckSubjectAsync(DvCheckSubjectDTO CheckSubject)
        {
            var n = AutoMapperExt.MapTo<DV_Check_Subject>(CheckSubject);
            await _dvCheckSubjectRepository.InsertAsync(n);
            return 1;
        }

        public async Task<int> UpdateCheckSubjectAsync(UpdateDvCheckSubjectDTO CheckSubject)
        {
            var model = AutoMapperExt.MapTo<DV_Check_Subject>(CheckSubject);
            await _dvCheckSubjectRepository.UpdateAsync(model);
            return 1;

        }

        public async Task<int> DeleteCheckSubjectAsync(int id)
        {
            await _dvCheckSubjectRepository.DeleteAsync(id, true);
            return 1;
        }
    }
}
